package org.example;

import org.example.generator.DataGenerator;
import org.example.entity.CustomerInfo;
import org.example.entity.CustomerOrder;
import org.example.entity.CustomerRepayPlan;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试还款分布逻辑
 */
public class TestRepayDistribution {
    
    public static void main(String[] args) {
        System.out.println("=== 还款分布测试 ===");
        
        int testCount = 1000; // 测试1000个订单
        Map<String, Integer> repayTypeCount = new HashMap<>();
        
        System.out.println("生成 " + testCount + " 个订单进行还款分布测试...");
        
        for (int i = 0; i < testCount; i++) {
            // 生成客户信息
            CustomerInfo customer = DataGenerator.generateCustomerInfo(LocalDate.of(2023, 7, 1));
            
            // 生成订单
            CustomerOrder order = DataGenerator.generateCustomerOrder(customer, LocalDate.of(2023, 7, 1), "浩瀚小贷");
            order.setCustomerId(customer.getId());
            
            // 生成还款计划
            List<CustomerRepayPlan> repayPlans = DataGenerator.generateRepayPlan(order);
            
            // 统计还款类型
            for (CustomerRepayPlan plan : repayPlans) {
                if ("已结清".equals(plan.getRepayStatus())) {
                    int overdueDays = plan.getOverdueDays();
                    if (overdueDays == 0) {
                        repayTypeCount.put("按时还款(0天)", repayTypeCount.getOrDefault("按时还款(0天)", 0) + 1);
                    } else if (overdueDays >= 1 && overdueDays <= 3) {
                        repayTypeCount.put("1-3天还款", repayTypeCount.getOrDefault("1-3天还款", 0) + 1);
                    } else {
                        repayTypeCount.put("逾期还款(>3天)", repayTypeCount.getOrDefault("逾期还款(>3天)", 0) + 1);
                    }
                } else if ("逾期".equals(plan.getRepayStatus())) {
                    repayTypeCount.put("逾期未还", repayTypeCount.getOrDefault("逾期未还", 0) + 1);
                } else {
                    repayTypeCount.put("未到期", repayTypeCount.getOrDefault("未到期", 0) + 1);
                }
            }
        }
        
        // 计算总还款计划数
        int totalPlans = repayTypeCount.values().stream().mapToInt(Integer::intValue).sum();
        
        // 输出结果
        System.out.println("\n=== 还款分布结果 ===");
        for (Map.Entry<String, Integer> entry : repayTypeCount.entrySet()) {
            double percentage = (double) entry.getValue() / totalPlans * 100;
            String target = getTargetPercentage(entry.getKey());
            System.out.printf("%s: %d (%.1f%%) - 目标: %s\n", 
                entry.getKey(), entry.getValue(), percentage, target);
        }
        
        System.out.println("\n=== 分布验证 ===");
        verifyDistribution("按时还款", repayTypeCount.get("按时还款(0天)"), totalPlans, 85.0);
        verifyDistribution("1-3天还款", repayTypeCount.get("1-3天还款"), totalPlans, 10.0);
        verifyDistribution("其他情况", repayTypeCount.get("逾期还款(>3天)") + repayTypeCount.get("逾期未还"), totalPlans, 5.0);
        
        System.out.println("\n=== 测试完成 ===");
        System.out.println("总计还款计划数: " + totalPlans);
        System.out.println("测试订单数: " + testCount);
    }
    
    private static String getTargetPercentage(String repayType) {
        switch (repayType) {
            case "按时还款(0天)": return "85%";
            case "1-3天还款": return "10%";
            case "逾期还款(>3天)": return "部分5%";
            case "逾期未还": return "部分5%";
            case "未到期": return "正常状态";
            default: return "0%";
        }
    }
    
    private static void verifyDistribution(String name, Integer actual, int total, double targetPercentage) {
        if (actual == null) actual = 0;
        double actualPercentage = (double) actual / total * 100;
        double diff = Math.abs(actualPercentage - targetPercentage);
        String status = diff <= 3.0 ? "PASS" : "FAIL";
        System.out.printf("%s %s: Actual %.1f%% vs Target %.1f%% (Diff %.1f%%)\n", 
            status, name, actualPercentage, targetPercentage, diff);
    }
}




